<template>
  <div id="shareWordList">
    <!-- 上部搜索条件 -->
    <topSearch :form-inline="formInline" @toSearch="toSearch"></topSearch>
    <!-- 操作按钮部分 -->
    <div class="table-conBtn">
      <el-button
        v-permission="'wh:system:shareLanguage:add'"
        type="success"
        icon="el-icon-plus"
        @click="addBtn"
      >
        新增
      </el-button>
      <el-button
        v-permission="'wh:system:shareLanguage:import'"
        type="warning"
        icon="el-icon-upload2"
        @click="importBtn"
      >
        导入
      </el-button>
      <el-button
        v-permission="'wh:system:shareLanguage:export'"
        type="warning"
        @click="exportFile"
      >
        导出
        <i class="el-icon-download el-icon--right"></i>
      </el-button>
      <el-button
        v-permission="'wh:system:shareLanguage:remove'"
        type="warning"
        plain
        @click="deleteMult"
      >
        批量删除
      </el-button>
      <freshCom @searchList="searchList"></freshCom>
    </div>
    <!-- 表格部分 -->
    <el-table
      id="share-box"
      v-loading="loading"
      :data="tableData"
      style="width: 100%"
      @selection-change="handleSelectionChange"
      max-height="490"
      ref="table"
    >
      <el-table-column type="selection" width="55"></el-table-column>
      <el-table-column type="index" width="260" label="序号">
        <template slot-scope="scope">
          {{
            scope.$index
              | filterPage(searchParams.pageNum, searchParams.pageSize)
          }}
        </template>
      </el-table-column>
      <el-table-column prop="type" label="类型" width="280">
        <template slot-scope="scope">
          <span
            v-if="scope.row.type"
            class="tag-span"
            :class="scope.row.type | filterClass('shareType')"
            >{{ scope.row.type | filterName('shareType') }}</span
          >
        </template>
      </el-table-column>
      <el-table-column
        prop="title"
        label="标题"
        min-width="360"
      ></el-table-column>
      <el-table-column prop="college" label="学院" width="280">
        <template slot-scope="scope">
          <span
            v-if="scope.row.collegeId"
            class="tag-span"
            :class="scope.row.collegeId | filterClass('shareCollegeType')"
            >{{ scope.row.collegeId | filterName('shareCollegeType') }}</span
          >
        </template>
      </el-table-column>
      <el-table-column
        prop="createdDate"
        label="创建时间"
        width="280"
      ></el-table-column>
      <!--此处不能添加fixed="right"，会导致导出表格数据重复-->
      <el-table-column label="操作" width="120">
        <template slot-scope="scope">
          <el-button
            v-permission="'wh:system:shareLanguage:remove'"
            type="text"
            @click="deleteOne(scope.row.id)"
          >
            删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>
    <!--分页-全局组件-->
    <pageCom
      :searchParams="searchParams"
      :total="total"
      @searchList="searchList"
    ></pageCom>
    <!-- 新增弹窗 -->
    <addDialog
      v-if="addDialogData.isShow"
      :dialog-data="addDialogData"
      @refresh="refresh"
    ></addDialog>
    <!-- 导入弹窗 -->
    <importDialog
      v-if="importDialogData.isShow"
      :dialog-data="importDialogData"
      @refresh="refresh"
      @showError="showError"
    ></importDialog>
    <!-- 导入失败弹窗 -->
    <errorDialog
      v-if="errorDialogData.isShow"
      :dialog-data="errorDialogData"
      from="shareWord"
    ></errorDialog>
  </div>
</template>
<script>
import topSearch from '@component/shareWordManagement/shareManage/topSearch' // 上部搜索条件
import importDialog from '@common/importDialog' // 导入弹窗
import errorDialog from '@common/errorDialog' // 导入失败弹窗
import addDialog from '@component/shareWordManagement/shareManage/addDialog' // 新增弹窗
import { saveScrollMixins } from '@/mixins/saveScroll'
import {
  getSearchList,
  deleteSingleRequest,
  deleteMultRequest,
  downUrl,
  importUrl
} from '@/api/shareWordManagement/shareWordAPI'
export default {
  name: 'shareWord',
  msg: '分享语管理-列表页',
  mixins: [saveScrollMixins],
  components: {
    topSearch,
    importDialog,
    addDialog,
    errorDialog
  },
  data() {
    return {
      loading: true,
      total: 0,
      // 搜索条件
      formInline: {
        type: '', // 用户类型,默认全部
        collegeId: '', // 学院,默认全部
        title: '', // 标题
        startTime: '', // 开始时间
        endTime: '', // 结束时间
        createTime: []
      },
      // 表格数据
      tableData: [],
      // 分页数据
      searchParams: {
        pageNum: 1, // 当前页数
        pageSize: 10 // 页面显示条数
      },
      // 传递给新增弹框的数据
      addDialogData: {
        isShow: false, // 控制弹框显示
        params: '' // 参数
      },
      // 传递给导入弹框的数据
      importDialogData: {
        title: '创建分享语管理',
        isShow: false, // 控制弹框显示
        params: '' // 参数
      },
      // 导入失败弹框的数据
      errorDialogData: {
        isShow: false, // 控制弹框显示
        warningText: '' // 参数
      },
      // 选择框选中的数据
      multipleSelection: []
    }
  },
  activated() {
    this.toTableScrollHeight()
  },
  mounted() {
    this.searchList()
    this.$nextTick(() => {
      setTimeout(() => {
        this.setTableScrollHeight()
      }, 1000)
    })
  },
  methods: {
    /* 获取权限 */
    getPower() {
      let id = this.$route.query.mId
      this.getCurPageMenu(id).then((res) => {
        this.powerAry = res
        this.canShowPage = true
        if (!res.includes(this.powerStr)) {
          this.canGetList = false
          this.alertPower(this.powerStr)
        }
        this.searchList()
      })
    },
    /* 获取列表 */
    searchList() {
      this.loading = true
      let obj = Object.assign({}, this.searchParams, this.formInline)
      delete obj.createTime
      let params = obj
      getSearchList(
        params,
        (response) => {
          this.loading = false
          this.tableData = response.rows
          this.total = response.total
        },
        (response) => {
          this.loading = false
          this.$message({
            message: response.message,
            type: 'warning'
          })
        }
      )
    },
    // 新增
    addBtn() {
      this.addDialogData.isShow = true // 显示新增弹框
    },
    // 导入
    importBtn() {
      this.importDialogData.url = importUrl() // 导入接口
      this.importDialogData.downUrl = downUrl() // 下载模板接口
      this.importDialogData.isShow = true // 显示导入弹框
    },
    /* 导入失败弹窗 */
    showError(text) {
      this.errorDialogData.isShow = true // 显示导入失败弹框
      this.errorDialogData.warningText = text // 显示导入失败弹框
    },
    /* 导出 */
    exportFile(type) {
      this.$confirm('确定导出所有分享语吗？', '系统提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(() => {
          let url = '/terminologies/share/export'
          let params = Object.assign({}, this.formInline)
          params.pageSize = 10000000
          delete params.createTime
          this.downFile('get', url, params, '分享语')
        })
        .catch(() => {})
    },
    // 多个删除按钮
    deleteMult() {
      if (this.multipleSelection.length <= 0) {
        this.$message.warning('请至少选择一条数据！')
        return
      }
      this.$confirm(
        `确认要删除选中的${this.multipleSelection.length}条数据吗?`,
        '删除提示',
        {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }
      ).then(() => {
        let ids = []
        if (this.multipleSelection.length === 1) {
          ids[0] = this.multipleSelection[0].id
          this.delItem(ids)
        } else {
          this.multipleSelection.forEach((item) => {
            ids.push(item.id)
          })
          this.delMulRequest(ids)
        }
      })
    },
    // 单个删除按钮
    deleteOne(id) {
      this.$confirm('确定删除该条分享语管理信息吗？', '删除提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        this.delItem([id])
      })
    },
    /* 删除单个请求 */
    delItem(ids) {
      deleteSingleRequest(
        ids,
        (response) => {
          this.searchList()
        },
        (response) => {
          this.$message({
            message: response.message,
            type: 'warning'
          })
        }
      )
    },
    // 删除多个请求
    delMulRequest(ids) {
      let params = { ids }
      deleteMultRequest(
        params,
        (response) => {
          this.searchList()
        },
        (response) => {
          this.$message({
            message: response.message,
            type: 'warning'
          })
        }
      )
    },
    /* table选择 */
    handleSelectionChange(val) {
      this.multipleSelection = val
    },
    /* 换页 */
    changePage(page) {
      this.searchParams.pageNum = page
      this.searchList()
    },
    /* 新增后，刷新列表 */
    refresh() {
      this.formInline = {
        type: '',
        collegeId: '',
        title: '',
        startTime: '',
        endTime: '',
        createTime: []
      }
      this.toSearch()
    },
    /* 搜索 */
    toSearch() {
      this.searchParams.pageNum = 1
      this.searchList()
    }
  }
}
</script>
<style lang="less">
#shareWordList {
  .table-conBtn {
    .el-dropdown {
      margin: 0 10px;
    }
  }
  .el-table {
    margin-top: 20px;
  }
}
</style>
